#include "relay.h"
#include "systimer.h"

static Relay relay[RELAY_NUM] = {
    {RELAY_WIND_L, 0, GPIOA, GPIO_PIN_1},
    {RELAY_WIND_M, 0, GPIOA, GPIO_PIN_2},
    {RELAY_WIND_H, 0, GPIOA, GPIO_PIN_3},
    {RELAY_VALVE,  0, GPIOA, GPIO_PIN_4},
};

void relay_init(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);

    for(uint8_t i = 0; i < RELAY_NUM; i++)
    {
        gpio_init(relay[i].gpio_periph, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, relay[i].pin);
        gpio_bit_reset(relay[i].gpio_periph, relay[i].pin);
    }
}

void relay_set(uint8_t id, uint8_t state)
{
    if(state != relay[id].state)
    {
        relay[id].state = state;
        // 发送控制指令
        gpio_bit_write(relay[id].gpio_periph, relay[id].pin, (bit_status)state);
    }
}

uint8_t get_relay_state(uint8_t id)
{
    return relay[id].state;
}

// void relay_self_test(void)
// {
//     for(uint8_t i = 0; i < RELAY_NUM; i++)
//     {
//         relay_set((RelayID)i, 1);
//         delay_ms(5000);
//         relay_set((RelayID)i, 0);
//         delay_ms(5000);
//     }
// }
